package com.xinsoft.controller.basic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.entity.po.SysPageField;
import com.xinsoft.service.SysPageFieldService;
import com.xinsoft.service.SysPageService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SysPageFieldController
 * @Description 页面字段控制层
 * @Author lijie
 * @Date 2023年5月18日10:18:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/field")
@Api(value = "SysPageController", tags = {"页面字段控制层"})
public class SysPageFieldController {
    @Autowired
    private SysPageService pageService;

    @Autowired
    private SysPageFieldService fieldService;

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 页面字段-新增
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @SysLog(title = "页面字段-新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增")
    @PostMapping("/add")
    public Response<?> add(@RequestBody SysPageField pageField) {
        // 参数校验
        ValidatorUtils.validateEntity(pageField);
//        SysPageField oldPage = fieldService.getOne(new QueryWrapper<SysPageField>()
//                .eq("field_code", pageField.getFieldCode())
//                .or()
//                .eq("field_name", pageField.getFieldName()));
//        if (oldPage != null) {
//            return Response.fail("该字段名称或显示名称已存在,请不要重复添加");
//        }
        return Response.succeed(fieldService.save(pageField));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("更新")
    @SysLog(title = "页面字段-编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody SysPageField pageField) {
        // 参数校验
        SysPageField oldPage = fieldService.getOne(new QueryWrapper<SysPageField>()
                .eq("field_code", pageField.getFieldCode())
                .eq("page_id",pageField.getPageId()));
        // 同样一个页面，并且字段id不同，才算重复
        if (oldPage != null && !oldPage.getId().equals(pageField.getId()) && pageField.getPageId().equals(oldPage.getPageId())) {
            return Response.fail("该页面名称或编码已存在,请不要重复添加");
        }
        ValidatorUtils.validateEntity(pageField);
        return Response.succeed(fieldService.updateById(pageField));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(fieldService.getById(id));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @SysLog(title = "页面字段-删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
		fieldService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 通过页面code取字段列表不分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/list")
    public Response<?> list(@RequestParam(value = "pageCode", defaultValue = "", required = false) String pageCode) {
        QueryWrapper<SysPageField> queryWrapper = new QueryWrapper<>();
        //查询条件
        if (StrUtil.isNotBlank(pageCode)) {
            queryWrapper.like("page_code", pageCode);
        }
        return Response.succeed(fieldService.list(queryWrapper));
    }
}
